#!/bin/bash
. /etc/sipman.conf
eval `$SIM_PROCCGI $*`
#----------------- procedures ----------------


printLongIp ()
{
IP=$1
COUNT=1
for NUM in `echo "$IP" | awk -F. '{ print $1 " " $2 " " $3 " " $4 }'`
do
  if [ "${#NUM}" -eq "1" ]; then
    printf 00$NUM
  elif [ "${#NUM}" -eq "2" ]; then
    printf 0$NUM
  else
    printf $NUM
  fi
  if [ "$COUNT" -lt "4" ]; then
    printf "."
  fi
  COUNT=`expr $COUNT + 1`
done
}

testNetName ()
{
  NETFILE=$1
  NETADDR=`echo $NETFILE | awk -F- '{ print $2 }'`
  NETMASK=`echo $NETFILE | awk -F- '{ print $3 }'`
  grep -l "NETWORK: $NETADDR" $SIM_SUBNETD/* | while read FILE
  do
    if [ "`grep NETWORK: $FILE | awk '{ print $2 }'`" = "$NETADDR" ]; then
      NAMESTR=`grep NAME: $FILE`
      printf "${NAMESTR:6}"
      break
    fi
  done
}

runMtr ()
{
  local IPADDR=$1
  echo "<H3>Mtr traceroute results for $IPADDR.</H3>
        <TABLE>"
  FIRST=yes
  $SIM_MTR -c 3 -r -n $IPADDR | while read V1 V2 V3 V4 V5 V6 V7 V8 V9
  do
    echo "<TR ALIGN=right>"
    if [ "$FIRST" = "yes" ]; then
      echo "<TD ALIGN=left COLSPAN=4>Tracing from <B>$V2</B></TD>"
    elif [ "$FIRST" = "no" ] && [ "$V1" != "???" ]; then
      echo "<TD>$V1</TD>
            <TD ALIGN=left>`$SIM_HOST $V2 | awk '{ print $5 }'`</TD>
            <TD><A HREF=$SCRIPT_NAME?mon+addnode+$V2>(+)</A></TD>
            <TD ALIGN=right BGCOLOR=`DispIpStatusColor $V2`>$V2</TD>"
    else
      echo "<TD>$V1</TD><TD></TD><TD></TD>
            <TD ALIGN=right BGCOLOR=`DispIpStatusColor $V2`>$V2</TD>"
    fi
    echo "<TD>$V3</TD>
          <TD>$V4</TD>
          <TD>$V5</TD>
          <TD>$V6</TD>
          <TD>$V7</TD>
          <TD>$V8</TD>
          <TD>$V9</TD>
          </TR>"
    FIRST=no
  done
  echo "</TABLE>"
}

displayColorTable ()
{
  echo "<TABLE><TR><TD><TABLE>
        <TR><TD WIDTH=90>Node type</TD>"
  NUM=1
  while [ "${NETNAME[$NUM]}" != "" ]
  do
    echo "<TD BGCOLOR=${NETBG[$NUM]}>${NETNAME[$NUM]}</TD>"
    NUM=`expr $NUM + 1`
  done
  echo "<TD BGCOLOR=${NETBG[0]}>${NETNAME[0]}</TD></TR>
        </TABLE>"

  echo "<TABLE><TR><TD WIDTH=90>Reccord type</TD><TD>"
  NUM=1
  while [ "${RECNAME[$NUM]}" != "" ]
  do
    echo "<TD BGCOLOR=${RECCOLOR[$NUM]}><FONT COLOR=${RECFONTCOLOR[$NUM]}>${RECTYPESTR[$NUM]} = ${RECNAME[$NUM]}</FONT></TD>"
    NUM=`expr $NUM + 1`
  done
  echo "</TR>
        </TABLE>"


  echo "<TABLE><TR><TD WIDTH=90>Status color</TD>"
  NUM=1
  while [ "${NMAPNAME[$NUM]}" != "" ]
  do
    echo "<TD BGCOLOR=${NMAPCOLOR[$NUM]}>${NMAPNAME[$NUM]}</TD>"
    NUM=`expr $NUM + 1`
  done
  echo "</TR></TABLE>"

  echo "</TD><TD>"  # switch to the seccond collum.

  echo "<TABLE><TR><TD>DNS type color</TD>"
  NUM=1
  while [ "${DNSTYPE[$NUM]}" != "" ]
  do
    echo "<TD BGCOLOR=${DNSCOLOR[$NUM]}>${DNSTYPE[$NUM]}</TD>"
    NUM=`expr $NUM + 1`
  done
  echo "</TR></TABLE>"

  echo "<TABLE><TR><TD>Mac Type color</TD>"
  NUM=1
  while [ "${DHCPTYPE[$NUM]}" != "" ]
  do
    echo "<TD BGCOLOR=${DHCPCOLOR[$NUM]}>${DHCPTYPE[$NUM]}</TD>"
    NUM=`expr $NUM + 1`
  done

  echo "</TR></TABLE></TD></TR></TABLE>"

}
testMacColor ()
{
  local MACTYPE=$1
  NUM=1
  MACCOLOR=""
  while [ "${DHCPTYPE[$NUM]}" != "" ]
  do
    if [ "$MACTYPE" = "${DHCPTYPE[$NUM]}" ]; then
      local MACCOLOR=${DHCPCOLOR[$NUM]}
    fi
    NUM=`expr $NUM + 1`
  done
  if [ "$MACCOLOR" = "" ]; then
    MACCOLOR=${DHCPCOLOR[0]}
  fi
  printf $MACCOLOR
}

getfixedinfo ()
{
grep -h " $1 " $SIM_FIXEDINFOD/* | while read TYPE FIP FINFO
do
  if [ "$1" = "$FIP" ]; then
    printf "<TR><TD>$TYPE:</TD><TD>($FINFO)</TD></TR>"
  fi
done
}

getmacinfo ()
{
MAC=$1
HTML=$2
if [ "$MAC" != "fixed" ]; then
  MACFILE="$SIM_MACINFOD/${MAC:0:8}/$MAC"
  if [ -r "$MACFILE" ]; then
    cat $MACFILE | grep CAM: | tail -n 1 | while read IDATE ICAM ISYSNAME IVLANNUM IVLANNAME IPORTNAME IPORTDESCR
    do
      printf "<TR><TD>Switchport:</TD><TD>$ISYSNAME $IPORTNAME $IPORTDESCR</TD></TR>"
      #grep "$ISYSNAME $IPORTNAME " /var/sipmon/ILONA.txt | while read A1 A2 REST
      #do
      #  printf "<TR><TD>Ilona:</TD><TD>$REST</TD></TR>"
      #done
    done
    cat $MACFILE | grep " USR: " | tail -n 3 | while read IDATE IUSR IPNUM IREST
    do
      printf "<TR><TD>Users:</TD><TD>$IREST</TD></TR>"
    done
    OID=`grep " OID: " $MACFILE | awk '{ print $4 " (" $3 ")" }'`
    if [ "$OID" != "" ]; then
      printf "<TR><TD>Snmp OID:</TD><TD>$OID</TD></TR>"
    fi
  fi
fi
}
getSipmanInfo ()
{
  local IPADDR=$1
  NETDIR=$SIM_IPDATAB/`echo $IPADDR | sed 's#\.#/#g'`
  COMMENT="`readFile $NETDIR/comment.txt`"
  if [ "$COMMENT" != "-" ]; then
    echo "<TR><TD>Comment:</TD><TD>$COMMENT</TD></TR>" 
  fi
}

displayNamedInfo ()
{
  DNSNAME=$1
  if [ -f $SIM_NAMEDMONDIR/names/$DNSNAME ]; then
    NUM=`cat $SIM_NAMEDMONDIR/names/$DNSNAME | wc -l`
    DATE=`ls -l $SIM_NAMEDMONDIR/names/$DNSNAME | awk '{ print $6 "-" $7 }'`
  else
    NUM="-"
  fi
  if [ "$NUM" != "-" ]; then
    printf "<A HREF=$SIM_NAMEDMONURL?named+name+$DNSNAME TARGET=_blank>$DNSNAME ($NUM / $DATE)</A>"
  else
    printf "$DNSNAME"
  fi
}

DispDnsEntry ()
{
  IPADDR=$1
  DNSINFO=$SIM_DNSCACHE
  TEST=`grep " A $IPADDR " $DNSINFO`
  if [ "$TEST" != "" ]; then
    printf "<TABLE BORDER=0>
          <TR BGCOLOR=#EEEEEE>
          <TD>DNS name</TD>
          <TD>Adresses</TD>
          <TD>Aliases</TD>
          <TD>Services</TD>
          </TR>"

    grep " A $IPADDR " $DNSINFO | while read VAR DNSNAME A IPRESULT REST
    do
      printf "<TR><TD VALIGN=top>`displayNamedInfo $DNSNAME`</TD><TD VALIGN=top>"
      grep " $DNSNAME A" $DNSINFO | while read VAR DNSRESULT A IPRESULT REST
      do
        BGCOLOR=`DispIpStatusColor $IPRESULT`
        if [ "$IPADDR" != "$IPRESULT" ]; then
          printf "<TABLE BORDER=0>
                  <TR><TD BGCOLOR=$BGCOLOR>
                  <A HREF=$SCRIPT_NAME?mon+nodestatus+$IPRESULT>$IPRESULT</A>
                  </TD></TR></TABLE>
                  "
        else
          printf "$IPRESULT<BR>"
        fi
      done
      printf "</TD><TD VALIGN=top>"
      grep "CNAME $DNSNAME " $DNSINFO | while read VAR CNAME VAR DNSRESULT REST
      do
          echo "`displayNamedInfo $CNAME`<BR>"
      done
      printf "</TD><TD>"
      grep " $DNSNAME SRV " $DNSINFO | while read VAR DNSNAME SRV NUM1 NUM2 NUM3 SERVICE
      do
        echo "$NUM1 $NUM2 $NUM3 $SERVICE<BR>"
      done
      printf "</TD></TR>"
    done
    printf "</TABLE>"
  fi
}

printIndexEntry ()
{
DIRECTORY=$1
FILE=$2
TYPE=$3
PARAM=$4

      SUBNET=`echo $FILE | awk -F- '{ print $2 }'`
      echo "<TD><A HREF=$SCRIPT_NAME?mon+display+-+$FILE>$SUBNET</TD>"
      sort -n -k 2 $DIRECTORY/$FILE | while read KEY NUM TTYPE UP DOWN REST
      do
        if [ "$NUM" = "99" ]; then
          NUM=0
        fi
        if [ "$TYPE" = "all" ] || [ "$PARAM" = "$TTYPE" ]; then
          if [ "${NETTYPE[$NUM]}" != "crit" ]; then
            if [ "$UP" -gt "0" ] && [ "$DOWN" = "0" ]; then
              NODECOLOR=limegreen
            elif [ "$UP" -eq "0" ] && [ "$DOWN" -gt "0" ]; then
              NODECOLOR=red
            elif [ "$UP" -gt "$DOWN" ]; then
              NODECOLOR=#FFFFBB
            else
              NODECOLOR=#FFC588
            fi
          else
            if [ "$UP" -gt "0" ] && [ "$DOWN" = "0" ]; then
              NODECOLOR=limegreen
            elif [ "$UP" -gt "0" ] && [ "$DOWN" -eq "1" ]; then
              NODECOLOR=yellow
            elif [ "$UP" -gt "0" ] && [ "$DOWN" -eq "2" ]; then
              NODECOLOR=orange
            else
              NODECOLOR=red
            fi
          fi
          echo "<TD BGCOLOR=${NETBG[$NUM]}>$TTYPE</TD><TD BGCOLOR=$NODECOLOR>$UP / $DOWN</TD>"
        fi
      done
}

testIpAddr ()
{
  local IPADDR=$1
  for NUM in `echo $IPADDR | sed 's/\./ /g'`
  do
    if [ "$NUM" -ge "0" ] && [ "$NUM" -le "255" ]; then
      printf "x"
    else
      printf "nok"
    fi
  done
}

addNewNode ()
{
IPADDR=$1
OPTION=$2
  if [ "`testIpAddr $IPADDR`" = "xxxx" ]; then
    NETDIR=$SIM_IPDATAB/`echo $IPADDR | sed 's#\.#/#g'`
    if [ -d $NETDIR ]; then
      echo "Node $IPADDR already defined.<BR>"
    else
      echo "Adding $IPADDR.<BR>"
      $SIM_SUDO $SIM_UPDATET $REMOTE_USER addnode $IPADDR
      $SIM_SUDO $SIM_UPDATET $REMOTE_USER modSipmanRectype $IPADDR mon
#     echo "Updating info for: $IPADDR.<BR>
#             </CENTER><PRE>"
#     $SIM_SUDO $SIM_IMPORTINFO searchlist $IPADDR
#     echo "</PRE><CENTER>"
   fi
   if [ "$OPTION" != "nodisp" ]; then
     dispNodeStatus $IPADDR
   fi
  else
    echo "$IPADDR is not an ipaddr, testing if it is an resolvable name.<BR>"
    # www.cisco.com has address 198.133.219.25
    $SIM_HOST $IPADDR | while read NAME V2 V3 IPADDR
    do
      if [ "$V2 $V3" = "has address" ]; then
        addNewNode $IPADDR
      else
        echo "The dns server resolved $NAME $V2 $V3 $IPADDR, sorry."
      fi
    done
  fi
}

displaySubHEntries ()
{
local NETWORK=$1
local OPTION=$2
if [ "$NETWORK" != "" ]; then
  NETDIR=$SIM_IPDATAB/`echo $NETWORK | sed 's#\.#/#g'`
  DISPNET="$NETWORK."
else
  NETDIR=$SIM_IPDATAB
  DISPNET=""
fi
ls $NETDIR | sort -n | while read DIR
do
  if [ -f $NETDIR/$DIR ]; then
    # display the content of files, to help in the debugging process
    local CONTENT=`cat $NETDIR/$DIR`
    echo " <FONT COLOR=red>$DIR ($CONTENT)</FONT> "
    rm $NETDIR/$DIR
  else
    echo ". "
    DISPDIR=$DISPNET$DIR
    if [ "$OPTION" = "sub" ]; then
      echo "<A HREF=$SCRIPT_NAME?mon+subnets+ip+$DISPDIR>$DISPDIR</A><A HREF=$SCRIPT_NAME?mon+displaysubnet+ip+$DISPDIR>(*)</A> ."
    else
      echo "<A HREF=$SCRIPT_NAME?mon+displaysubnet+ip+$DISPDIR>$DISPDIR</A> ."
    fi
  fi
done
}

displaySubnetHeader ()
{
echo "<TABLE>
      <TR BGCOLOR=#FFFF66 ALIGN=center>
      <TD>
      <A HREF=$SCRIPT_NAME?mon+displaysubnet+ip+root>All networks</A> / "

local NETWORK=$1
displaySubHEntries "" sub
if [ -d $SIM_IPDATAB/`echo $NETWORK | sed 's#\.#/#g'` ]; then
TEST="`echo $NETWORK | awk -F. '{ print $1 }'`" 
fi
if [ "$TEST" != "" ]; then
  echo "</TD></TR>
        <TR BGCOLOR=#FFFF99 ALIGN=center><TD>"
  displaySubHEntries $TEST sub
fi
TEST="`echo $NETWORK | awk -F. '{ print $2 }'`"
if [ "$TEST" != "" ]; then
  echo "</TD></TR>
        <TR BGCOLOR=#FFFFCC ALIGN=center><TD>"
  displaySubHEntries `echo $NETWORK | awk -F. '{ print $1 "." $2 }'` nosub
fi
echo "</TD></TR></TABLE>"
}

readFile ()
{
  FILE=$1
  if [ -s $FILE ]; then
    cat $FILE
#   $(< $FILE)
  else
    printf "-"
  fi
}

testNodeTypeColor ()
{
local NODETYPE=$1
  NUM=1
  TYPECOLOR=""
  while [ "${NETNAME[$NUM]}" != "" ]
  do
    if [ "$NODETYPE" = "${NETNAME[$NUM]}" ]; then
      local TYPECOLOR=${NETBG[$NUM]}
    fi
    NUM=`expr $NUM + 1`
  done
  if [ "$TYPECOLOR" = "" ]; then
    TYPECOLOR=${NETBG[0]}
  fi
  printf $TYPECOLOR
}

walkSubnetEntries ()
{
  local NETWORK=$1
  if [ "${NETWORK:0:1}" = "." ]; then
    NETWORK="${NETWORK:1}"
  fi
  local CACHEFILE=$2
# line for debugging.
# echo "<TR><TD COLSPAN=4> -$NETWORK-$CACHEFILE</TD></TR>"
  TEST="`echo $NETWORK | awk -F. '{ print $3 }'`"
  if [ "$NETWORK" = "" ]; then
    NETDIR=$SIM_IPDATAB
  else
    NETDIR=$SIM_IPDATAB/`echo $NETWORK | sed 's#\.#/#g'`
  fi
  if [ "$TEST" != "" ]; then
    ls $NETDIR | sort -n | while read DIR
    do
      #echo "<TR><TD COLSPAN=12>Invallid: $NETWORK.$DIR ($NETDIR/$DIR).</TD></TR>"
      if [ -f "$NETDIR/$DIR" ]; then
        echo "<TR><TD COLSPAN=12>Invallid: $NETWORK.$DIR, removing.</TD></TR>"
        rm $NETDIR/$DIR
      else
        IPADDR=$NETWORK.$DIR
        displaySubnetEntry $IPADDR $CACHEFILE
      fi
    done
  else
  ls $NETDIR | sort -n | while read DIR
    do
      walkSubnetEntries $NETWORK.$DIR $CACHEFILE
    done
  fi
}

displaySubnetEntry ()
{
local IPADDR=$1
local CACHEFILE=$2
NETDIR=$SIM_IPDATAB/`echo $IPADDR| sed 's#\.#/#g'`
    NODETYPE=`readFile $NETDIR/nodetype.txt`
    TYPECOLOR=`testNodeTypeColor $NODETYPE`

    # color for ipaddr indicating wether an inaddr entry will be generated.
    if [ ! -f $NETDIR/in-addr.arpa.txt ]; then
      IPCOLOR=orange
    else
      IPCOLOR=$TYPECOLOR
    fi

    SIPMANMANTYPE=`readFile $NETDIR/managementtype.txt`
    case $SIPMANMANTYPE in
      (${MANTYPE[0]}) MANBGCOLOR=${MANCOLOR[0]} ; DMANSTR=${MANSTR[0]} ;;
      (${MANTYPE[1]}) MANBGCOLOR=${MANCOLOR[1]} ; DMANSTR=${MANSTR[1]} ;;
      (${MANTYPE[2]}) MANBGCOLOR=${MANCOLOR[2]} ; DMANSTR=${MANSTR[2]} ;;
      (${MANTYPE[3]}) MANBGCOLOR=${MANCOLOR[3]} ; DMANSTR=${MANSTR[3]} ;;
      (*)             MANBGCOLOR=orange ; DMANSTR=? ;;
    esac
#   echo "-$SIPMANMANTYPE-$MANBGCOLOR-"

    SIPMANRECTYPE=`readFile $NETDIR/sipmanrectype.txt`
    case $SIPMANRECTYPE in
      (${RECNAME[1]}) SIPMANTYPEBGCOLOR=${RECCOLOR[1]} ; SIPMANTYPESTR=${RECTYPESTR[1]} ;;
      (${RECNAME[2]}) SIPMANTYPEBGCOLOR=${RECCOLOR[2]} ; SIPMANTYPESTR=${RECTYPESTR[2]} ;;
      (${RECNAME[3]}) SIPMANTYPEBGCOLOR=${RECCOLOR[3]} ; SIPMANTYPESTR=${RECTYPESTR[3]} ;;
      (${RECNAME[4]}) SIPMANTYPEBGCOLOR=${RECCOLOR[4]} ; SIPMANTYPESTR=${RECTYPESTR[4]} ;;
      (${RECNAME[5]}) SIPMANTYPEBGCOLOR=${RECCOLOR[5]} ; SIPMANTYPESTR=${RECTYPESTR[5]} ;;
      (${RECNAME[6]}) SIPMANTYPEBGCOLOR=${RECCOLOR[6]} ; SIPMANTYPESTR=${RECTYPESTR[6]} ;;
      (${RECNAME[7]}) SIPMANTYPEBGCOLOR=${RECCOLOR[7]} ; SIPMANTYPESTR=${RECTYPESTR[7]} ;;
      (${RECNAME[8]}) SIPMANTYPEBGCOLOR=${RECCOLOR[8]} ; SIPMANTYPESTR=${RECTYPESTR[8]} ;;
      (*)             SIPMANTYPEBGCOLOR=$TYPECOLOR ; SIPMANTYPESTR="-" ;;
    esac
    if [ -f "$NETDIR/dhcpmacaddr.txt" ]; then
      MACADDR=`readFile $NETDIR/dhcpmacaddr.txt`
      MACTYPE=dhcp
    elif [ -f "$NETDIR/snmpmac.txt" ]; then
      MACADDR=`readFile $NETDIR/snmpmac.txt`
      MACTYPE=snmp
    elif [ -f "$NETDIR/arpmac.txt" ]; then
      MACADDR=`readFile $NETDIR/arpmac.txt`
      MACTYPE=arp
    else
      MACADDR=none
      MACTYPE=none
    fi
    MACCOLOR=`testMacColor $MACTYPE`
    if [ "$MACTYPE" = "dhcp" ] || [ "$MACTYPE" = "snmp" ]; then
      if [ -f "$NETDIR/arpmac.txt" ]; then
        ARPMAC=`readFile $NETDIR/arpmac.txt`
	if [ "$ARPMAC" != "$MACADDR" ]; then
	  MACCOLOR="red"
	fi
      fi
    fi

    NSTATUS=`readFile $NETDIR/pollstatus.txt`
    case ${NSTATUS:20} in
      (${NMAPNAME[1]})  STATUSBGCOLOR=${NMAPCOLOR[1]} ;;
      (${NMAPNAME[2]})  STATUSBGCOLOR=${NMAPCOLOR[2]} ;;
      (${NMAPNAME[3]})  STATUSBGCOLOR=${NMAPCOLOR[3]} ;;
      (${NMAPNAME[4]})  STATUSBGCOLOR=${NMAPCOLOR[4]} ;;
      (${NMAPNAME[5]})  STATUSBGCOLOR=${NMAPCOLOR[5]} ;;
      (${NMAPNAME[6]})  STATUSBGCOLOR=${NMAPCOLOR[6]} ;;
      (${NMAPNAME[7]})  STATUSBGCOLOR=${NMAPCOLOR[7]} ;;
      (*)                 STATUSBGCOLOR=${NMAPCOLOR[0]} ;;
    esac
    if [ "${NSTATUS:0:10}" = "`date +%Y\-%m\-%d`" ]; then
      LINESTATUS=${NSTATUS:11:8}
    else
      LINESTATUS=${NSTATUS:0:10}
    fi
    if [ -f "$NETDIR/dnsname.txt" ]; then
      DNSREC=( `readFile $NETDIR/dnsname.txt` )
      DNSNAME="${DNSREC[1]}.${DNSREC[2]}"

      case ${DNSREC[0]} in
        (${DNSTYPE[1]})     DNSBGCOLOR=${DNSCOLOR[1]} ;;
        (${DNSTYPE[2]})     DNSBGCOLOR=${DNSCOLOR[2]} ;;
        (${DNSTYPE[3]})     DNSBGCOLOR=${DNSCOLOR[3]} ;;
        (${DNSTYPE[4]})     DNSBGCOLOR=${DNSCOLOR[4]} ;;
        (${DNSTYPE[5]})     DNSBGCOLOR=${DNSCOLOR[5]} ;;
        (${DNSTYPE[6]})     DNSBGCOLOR=${DNSCOLOR[6]} ;;
        (*)                 DNSBGCOLOR=${DNSCOLOR[0]} ;;
      esac
    else
      DNSNAME=noname.nodomain
      DNSBGCOLOR=white
    fi
    if [ "${#DNSNAME}" -gt "40" ]; then
      DNSNAME="${DNSNAME:0:38}..."
    fi
    HOSTNAME=`readFile $NETDIR/hostname.txt`
    if [ "$HOSTNAME" = "-" ]; then
      HOSTNAME=`readFile $NETDIR/dhcpname.txt`
    else
      if [ "${#HOSTNAME}" -gt "30" ]; then
        HOSTNAME="${HOSTNAME:0:28}..."
      fi
    fi
    SNMPOID=`readFile $NETDIR/snmpoid.txt`
    SNMPOID="`echo $SNMPOID | awk -F. '{ print $1 }'`" # AIX
    FIRSTMAC=`readFile $NETDIR/firstmac.txt`
    FMACDIR=$SIM_MACDATAB/$FIRSTMAC
    IPNETNO=`readFile $FMACDIR/ipnetnum.txt`
    INTNUM=`readFile $FMACDIR/intnum.txt`
    DMACDIR="$SIM_MACDATAB/$MACADDR"
    SWITCHINFO=`readFile $DMACDIR/switchport`
    if [ ! -f $SIM_SETTINGSD/lessColors-$REMOTE_USER ]; then
      echo "<TR BGCOLOR=$TYPECOLOR>
            <TD BGCOLOR=$MANBGCOLOR ALIGN=center>$DMANSTR</TD>
            <TD BGCOLOR=$IPCOLOR>$IPADDR</TD>
            <TD BGCOLOR=$MACCOLOR>$MACADDR</TD>
            <TD BGCOLOR=$SIPMANTYPEBGCOLOR>$SIPMANTYPESTR</TD>
            <TD>$NODETYPE</TD>
            <TD ALIGN=center BGCOLOR=$STATUSBGCOLOR><A HREF=$SCRIPT_NAME?mon+nodestatus+$IPADDR+$NETWORK>$LINESTATUS</A></TD>
            <TD ALIGN=right BGCOLOR=$DNSBGCOLOR>$DNSNAME</TD>
            <TD>$HOSTNAME</TD>
            <TD>${SNMPOID[0]}</TD>
            <TD>$IPNETNO/$INTNUM</TD>
            <TD>${SWITCHINFO[0]} ${SWITCHINFO[1]}</TD>
            </TR>"
    else
      echo "<TR BGCOLOR=$TYPECOLOR>
            <TD BGCOLOR=$MANBGCOLOR ALIGN=center>$DMANSTR</TD>
            <TD>$IPADDR</TD>
            <TD>$MACADDR</TD>
            <TD BGCOLOR=$SIPMANTYPEBGCOLOR>$SIPMANTYPESTR</TD>
            <TD>$NODETYPE</TD>
            <TD ALIGN=center BGCOLOR=$STATUSBGCOLOR><A HREF=$SCRIPT_NAME?mon+nodestatus+$IPADDR+$NETWORK>$LINESTATUS</A></TD>
            <TD>$DNSNAME</TD>
            <TD>$HOSTNAME</TD>
            <TD>${SNMPOID[0]}</TD>
            <TD>$IPNETNO/$INTNUM</TD>
            <TD>${SWITCHINFO[0]} ${SWITCHINFO[1]}</TD>
            </TR>"
    fi
#         <TD>`readFile $NETDIR/`</TD>
#         <TD>`readFile $NETDIR/snmpinfodate.txt`</TD>
#         <TD>`readFile $NETDIR/snmpcomm.txt`</TD>

#    echo "$NETDIR $MANBGCOLOR $TYPECOLOR $IPADDR $IPCOLOR $MACADDR $MACCOLOR $SIPMANTYPEBGCOLOR $SIPMANTYPESTR $NODETYPE ${NSTATUS:20} ${NSTATUS:0:19} $DNSBGCOLOR $DNSNAME $HOSTNAME ${SNMPOID[0]} $IPNETNO $INTNUM ${SWITCHINFO[0]} ${SWITCHINFO[1]}" >> $CACHEFILE
     echo "$IPADDR $MACADDR $NODETYPE $NSTATUS $DNSNAME $HOSTNAME ${SNMPOID[0]} $IPNETNO $INTNUM ${SWITCHINFO[0]} ${SWITCHINFO[1]}" >> $CACHEFILE
}

dispCacheTable ()
{
local CACHEFILE=$1
# Sorting of this file needs to be added.
local SORTKEY=$2
local OPTION=$3
local LINENUM=1
case $SORTKEY in 
  (mac)    CMDSTR="sort -k 6" ;;
  (type)   CMDSTR="sort -f -k 9" ;;
  (status) CMDSTR="sort -k 11" ;;
  (dns)    CMDSTR="sort -f -k 14" ;;
  (host)   CMDSTR="sort -f -k 15" ;;
  (oid)    CMDSTR="sort -f -k 16" ;;
  (int)    CMDSTR="sort -nr -k 17" ;;
  (switchport)   CMDSTR="sort -f -k 18" ;;
  (*)      CMDSTR=cat ;;
esac
$CMDSTR $CACHEFILE | while read NETDIR POLLCOLOR LINECOLOR IPADDR IPCOLOR MACADDR MACCOLOR TYPECOLOR SIPMANTYPE NODETYPE STATTYPE STATTIME DNSBGCOLOR DNSNAME HOSTNAME SNMPOID IPNETNO INTNUM SWITCHNAME SWITCHPORT
do
  NSTATUS=`readFile $NETDIR/pollstatus.txt`
  case ${NSTATUS:20} in
    (${NMAPNAME[1]})  STATUSBGCOLOR=${NMAPCOLOR[1]} ;;
    (${NMAPNAME[2]})  STATUSBGCOLOR=${NMAPCOLOR[2]} ;;
    (${NMAPNAME[3]})  STATUSBGCOLOR=${NMAPCOLOR[3]} ;;
    (${NMAPNAME[4]})  STATUSBGCOLOR=${NMAPCOLOR[4]} ;;
    (${NMAPNAME[5]})  STATUSBGCOLOR=${NMAPCOLOR[5]} ;;
    (${NMAPNAME[6]})  STATUSBGCOLOR=${NMAPCOLOR[6]} ;;
    (${NMAPNAME[7]})  STATUSBGCOLOR=${NMAPCOLOR[7]} ;;
    (*)                 STATUSBGCOLOR=${NMAPCOLOR[0]} ;;
  esac
  if [ "${NSTATUS:0:10}" = "`date +%Y\-%m\-%d`" ]; then
    LINESTATUS=${NSTATUS:11:8}
  else
    LINESTATUS=${NSTATUS:0:10}
  fi


  if [ ! -f $SIM_SETTINGSD/lessColors-$REMOTE_USER ]; then
  echo "<TR BGCOLOR=$LINECOLOR>
        <TD BGCOLOR=$POLLCOLOR ALIGN=right>$LINENUM</TD>
        <TD BGCOLOR=$IPCOLOR>$IPADDR</TD>
        <TD BGCOLOR=$MACCOLOR>$MACADDR</TD>
        <TD BGCOLOR=$TYPECOLOR>$SIPMANTYPE</TD>
        <TD>$NODETYPE</TD>
        <TD BGCOLOR=$STATUSBGCOLOR><A HREF=$SCRIPT_NAME?mon+nodestatus+$IPADDR+$NETWORK>$LINESTATUS</A></TD>
        <TD BGCOLOR=$DNSBGCOLOR>$DNSNAME</TD>
        <TD>$HOSTNAME</TD>
        <TD>$SNMPOID</TD>
        <TD>$IPNETNO/$INTNUM</TD>
        <TD>$SWITCHNAME $SWITCHPORT</TD>
        </TR>"
  else
    echo "<TR BGCOLOR=$LINECOLOR>
          <TD BGCOLOR=$POLLCOLOR ALIGN=right>$LINENUM</TD>
          <TD>$IPADDR</TD>
          <TD>$MACADDR</TD>
          <TD BGCOLOR=$TYPECOLOR>$SIPMANTYPE</TD>
          <TD>$NODETYPE</TD>
          <TD BGCOLOR=$STATUSBGCOLOR><A HREF=$SCRIPT_NAME?mon+nodestatus+$IPADDR+$NETWORK>$LINESTATUS</A></TD>
          <TD>$DNSNAME</TD>
          <TD>$HOSTNAME</TD>
          <TD>$SNMPOID</TD>
          <TD>$IPNETNO/$INTNUM</TD>
          <TD>$SWITCHNAME $SWITCHPORT</TD>
          </TR>"
  fi
  LINENUM=`expr $LINENUM + 1`      
done
}

dispTableHeader ()
{
  local ACTION=$1
  if [ "$NETWORK" = "" ]; then
    DISPNETWORK=root
  else
    DISPNETWORK=$NETWORK
  fi
  echo "<TR BGCOLOR=#BBBBBB>
        <TD></TD>
        <TD><A HREF=$SCRIPT_NAME?mon+$ACTION+ip+$DISPNETWORK>Ip addres</A></TD>
        <TD><A HREF=$SCRIPT_NAME?mon+$ACTION+mac+$DISPNETWORK>Mac addres</TD>
        <TD COLSPAN=2><A HREF=$SCRIPT_NAME?mon+$ACTION+type+$DISPNETWORK>Type</TD>
        <TD><A HREF=$SCRIPT_NAME?mon+$ACTION+status+$DISPNETWORK>Status</TD>
        <TD><A HREF=$SCRIPT_NAME?mon+$ACTION+dns+$DISPNETWORK>Dns name</TD>
        <TD><A HREF=$SCRIPT_NAME?mon+$ACTION+host+$DISPNETWORK>Host name</TD>
        <TD><A HREF=$SCRIPT_NAME?mon+$ACTION+oid+$DISPNETWORK>Snmp Oid name</TD>
        <TD><A HREF=$SCRIPT_NAME?mon+$ACTION+int+$DISPNETWORK>Int</TD>
        <TD><A HREF=$SCRIPT_NAME?mon+$ACTION+switchport+$DISPNETWORK>Port</TD>
         </TR>"
}
removeIfExists ()
{
local FILE=$1
if [ -f "$FILE" ]; then
  rm $FILE
fi
}
DispNodeLogging ()
{
  local LOGFILE=$1
  echo "$SIM_FORMATLOGGING $LOGFILE $USERLEVEL ipaddr 20 1"
  printf "<TABLE>"
  $SIM_FORMATLOGGING $LOGFILE $USERLEVEL ipaddr 20 1
  echo "</TABLE>"
}

DispIpInt ()
{
  local IPADDR=$1
  local NETDIR=$2
  if [ "`ls $NETDIR/ipint-*.txt 2>/dev/null`" != "" ]; then
    echo "<TR BGCOLOR=$COLORINT><TD>INT</TD><TD><TABLE>
          <TR BGCOLOR=#DDDDDD><TD>Ip addr</TD>
              <TD>Mask</TD>
              <TD>Intno</TD>
              <TD>Int name</TD>
              <TD>Int description</TD>
              <TD>Int type</TD>
              <TD>Mac addres</TD>
              <TD>Admin status</TD>
              </TR>"
    ls $NETDIR/ipint-*.txt | while read FILE
    do
      NIPADDR=`cat $FILE | awk '{ print $1 }'`
      STATUSBGCOLOR=`DispIpStatusColor $NIPADDR`
      REST="`cat $FILE`"
      if [ "$IPADDR" = "$NIPADDR" ]; then
        DISPSTR="$NIPADDR"
      else
        DISPSTR="<A HREF=$SCRIPT_NAME?mon+nodestatus+$NIPADDR+$NETWORK>$NIPADDR</A>"
      fi
      echo "<TR><TD BGCOLOR=$STATUSBGCOLOR ALIGN=center>$DISPSTR ${REST:${#NIPADDR}}</TR>"
    done
    echo "</TABLE></TD></TR>"
  fi
}
DispIpStatusColor ()
{
  NIPADDR=$1
  NNETDIR=$SIM_IPDATAB/`echo $NIPADDR | sed 's#\.#/#g'`
  NSTATUS=`readFile $NNETDIR/pollstatus.txt`
  case ${NSTATUS:20} in
    (${NMAPNAME[1]})  STATUSBGCOLOR=${NMAPCOLOR[1]} ; TYPE=E ;;
    (${NMAPNAME[2]})  STATUSBGCOLOR=${NMAPCOLOR[2]} ; TYPE=E ;;
    (${NMAPNAME[3]})  STATUSBGCOLOR=${NMAPCOLOR[3]} ; TYPE=E ;;
    (${NMAPNAME[4]})  STATUSBGCOLOR=${NMAPCOLOR[4]} ; TYPE=E ;;
    (${NMAPNAME[5]})  STATUSBGCOLOR=${NMAPCOLOR[5]} ; TYPE=E ;;
    (${NMAPNAME[6]})  STATUSBGCOLOR=${NMAPCOLOR[6]} ; TYPE=E ;;
#   (${NMAPNAME[7]})  STATUSBGCOLOR=${NMAPCOLOR[7]} ; TYPE=E ;;
    (*)               STATUSBGCOLOR=${NMAPCOLOR[0]} ; TYPE=U ;;
  esac
  echo $STATUSBGCOLOR
}
DispCdpNeighb ()
{
  local IPADDR=$1
  local NETDIR=$2
  if [ "`ls $NETDIR/cdpneighb-*.txt 2>/dev/null`" != "" ]; then
    echo "<TR BGCOLOR=$COLORCDP><TD>CDP</TD><TD><TABLE>
          <TR BGCOLOR=#DDDDDD><TD>Ip addr</TD>
              <TD COLSPAN=2>Source interface</TD>
              <TD>Name</TD>
              <TD>Interface</TD>
              <TD>Hardware</TD>
              </TR>"
    ls $NETDIR/cdpneighb-*.txt | while read FILE
    do
      NIPADDR=`cat $FILE | awk '{ print $1 }'`
      STATUSBGCOLOR=`DispIpStatusColor $NIPADDR`
      REST="`cat $FILE`"
      echo "<TR><TD BGCOLOR=$STATUSBGCOLOR ALIGN=center>
            <A HREF=$SCRIPT_NAME?mon+nodestatus+$NIPADDR+$NETWORK>$NIPADDR</A>
            ${REST:${#NIPADDR}}</TR>"
    done
    echo "</TABLE></TD></TR>"
  fi
}
dispMessageLog ()
{
local DISPTYPE=$1
local NUMLINES=$2
local SELECTION=$3
local REPEATS=$4
local CURRDATE=`date +%Y%m%d_%H%M%S`
echo "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0><TR><TD BGCOLOR=#F0F0F0><CENTER>Node types: "
if [ "$SELECTION" = "" ]; then
  SELECTION=all
fi
cd $SIM_MESSAGESDIR
ls messages-*-log | while read FILE
do
  TYPE=`echo "$FILE" | awk -F- '{ print $2 }'`
  if [ "$TYPE" = "$SELECTION" ]; then
    FONTCOL=red
  else
    FONTCOL=#333333
  fi
  echo ".  <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+$NUMLINES+$TYPE><FONT COLOR=$FONTCOL>$TYPE</FONT></A> ."
done
if [ "$NUMLINES" = "" ]; then
  NUMLINES=20
fi
echo "</TD></TR><TR><TD BGCOLOR=#F8F8F8><CENTER>Logging:
      <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+25+$SELECTION+$REPEATS>25</A> /
      <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+50+$SELECTION+$REPEATS>50</A> /
      <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+100+$SELECTION+$REPEATS>100</A> /
      <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+500+$SELECTION+$REPEATS>500</A> /
      <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+1000+$SELECTION+$REPEATS>1000</A> /
      <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+2000+$SELECTION+$REPEATS>2000</A> 
      lines, Type:
      <A HREF=$SCRIPT_NAME?mon+log+ipaddr+$NUMLINES+$SELECTION+$REPEATS>Ip addres</A> /
      <A HREF=$SCRIPT_NAME?mon+log+dnsname+$NUMLINES+$SELECTION+$REPEATS>Dns name</A> 
      Repeats:
      <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+$NUMLINES+$SELECTION>no</A> /
      <A HREF=$SCRIPT_NAME?mon+log+$DISPTYPE+$NUMLINES+$SELECTION+1>yes</A>.
      </TD></TR></TABLE>"
# printf "$DISPTYPE $NUMLINES"
# echo "$SIM_FORMATLOGGING $SIM_LOGPREFIX-$SELECTION-log $USERLEVEL $DISPTYPE $NUMLINES"
  printf "<TABLE>"
  $SIM_FORMATLOGGING $SIM_LOGPREFIX-$SELECTION-log $USERLEVEL $DISPTYPE $NUMLINES $REPEATS
echo "</TABLE>"
}

dispNodeStatus ()
{
  IPADDR=$1
  NETDIR=$SIM_IPDATAB/`echo $IPADDR | sed 's#\.#/#g'`
    if [ ! -d $NETDIR ]; then
      echo "$NETDIR not found."
      ADDNODE="<A HREF=$SCRIPT_NAME?mon+addnode+$IPADDR>Add node</A> -"
    else
      ADDNODE=""
    fi
    NODETYPE=`readFile $NETDIR/nodetype.txt`
    SIPMANRECTYPE=`readFile $NETDIR/sipmanrectype.txt`
    MANAGEMENTTYPE=`readFile $NETDIR/managementtype.txt`
    SNMPMAC=`readFile $NETDIR/snmpmac.txt`
    DHCPMAC=`readFile $NETDIR/dhcpmacaddr.txt`
    ARPMAC=`readFile $NETDIR/arpmac.txt`
    NSTATUS=`readFile $NETDIR/pollstatus.txt`
    DNSNAME=`readFile $NETDIR/dnsname.txt`
    HOSTNAME=`readFile $NETDIR/hostname.txt`
    DHCPNAME=`readFile $NETDIR/dhcpname.txt`
    SNMPOID=`readFile $NETDIR/snmpoid.txt`
    COMM=`readFile $NETDIR/snmpcomm.txt`
    CFGDATE=`readFile $NETDIR/$DIR/snmpinfodate.txt`
    FIRSTMAC=`readFile $NETDIR/firstmac.txt`
    FMACDIR=$SIM_MACDATAB/$FIRSTMAC
    IPNETNO=`readFile $FMACDIR/ipnetnum.txt`
    INTNUM=`readFile $FMACDIR/intnum.txt`
    SNMPCONTACT=`readFile $FMACDIR/snmpContact`
    SNMPLOCATION=`readFile $FMACDIR/snmpLocation`
    if [ "$DHCPMAC" != "-" ]; then
      TMAC=$DHCPMAC
    elif [ "$SNMPMAC" != "-" ]; then
      TMAC=$SNMPMAC
    else
      TMAC=$ARPMAC
    fi
    DMACDIR="$SIM_MACDATAB/$TMAC"
    SWITCHINFO=`readFile $DMACDIR/switchport`
    if [ "$SWITCHINFO" != "-" ]; then
      SWITCHNAME=`echo $SWITCHINFO | awk '{ print $1 }'`
      SWITCHIP=`cat $SIM_SWITCHINFO/devices/$SWITCHNAME/switchip 2>/dev/null | awk '{ print $1 }'`
      SWITCHNETDIR=$SIM_IPDATAB/`echo $SWITCHIP | sed 's#\.#/#g'`
      OIDSTRING=`cat $SWITCHNETDIR/snmpoid.txt | awk '{ print $2 }'`
      VENDOR=`cat $SIM_OIDHELPD/.$OIDSTRING/vendor`
      GRAPHINTNAME=`echo $SWITCHINFO | awk '{ print $1 "." $2 }' | tr [A-Z] [a-z] | sed 's#/#-#g'`
    fi
#   echo "DMACDIR=$DMACDIR, SWITCHINFO=$SWITCHINFO, SWITCHNAME=$SWITCHNAME, SWITCHIP=$SWITCHIP, SWITCHNETDIR=$SWITCHNETDIR, 
#                           OIDSTRING=$OIDSTRING, VENDOR=$VENDOR GRAPHINTNAME=$GRAPHINTNAME."
 
#   $SIM_PRINTMENU mon-nodestatus line mon $ACTION $IPADDR $NETWORK
    echo "<TABLE BORDER=0 CELLSPACING=0><TR><TD COLSPAN=3 BGCOLOR=#BBBBBB><CENTER>
            <A HREF=telnet://$IPADDR>Telnet</A> -
            <A HREF=ssh://$IPADDR>Ssh</A> -
            <A HREF=http://$IPADDR TARGET=_blank>Http</A>"
	    # Syslog search needs to be reimplemented (security / size file etc...
#           Search: Syslog (
#           <A HREF=$SCRIPT_NAME?file+search+syslog+messages+$IPADDR TARGET=_blank>ip addr</A>"
#           if [ "$DHCPMAC" != "-" ]; then
#             echo " /
#           <A HREF=$SCRIPT_NAME?file+search+syslog+messages+$DHCPMAC TARGET=_blank>mac addr</A> "
#           fi
#         echo ")"
#         <TABLE>
    echo "
          </CENTER>
          </TD></TR>
          <TR BGCOLOR=$COLORNODE><TD>NODE</TD><TD>
          <TABLE>
          <TR><TD VALIGN=top>Mac address:</TD><TD><TABLE>"
    FIRSTMAC=`readFile $NETDIR/firstmac.txt`
    if [ "$DHCPMAC" != "-" ]; then
      if [ "$DHCPMAC" != "$ARPMAC" ] && [ "$ARPMAC" != "-" ]; then
        MACCOLOR=red
      fi
      echo "<TR><TD>dhcp</TD><TD BGCOLOR=$MACCOLOR>$DHCPMAC</TD></TR>"
    fi
    if [ "$SNMPMAC" != "-" ]; then
      if [ "$SNMPMAC" != "$ARPMAC" ] && [ "$SNMPMAC" != "-" ]; then
        MACCOLOR=red
      fi
      echo "<TR><TD>snmp</TD><TD BGCOLOR=$MACCOLOR>$SNMPMAC</TD></TR>"
    fi
    if [ "$ARPMAC" != "-" ]; then
      echo "<TR><TD>arp</TD><TD>$ARPMAC</TD></TR>"
    fi
    if [ "$FIRSTMAC" != "-" ]; then
      echo "<TR><TD>firstmac</TD><TD>$FIRSTMAC</TD></TR>"
    fi

    echo "</TABLE>"
    echo "</TD></TR>
          <TR><TD>IP address:</TD><TD><A HREF=$SCRIPT_NAME?mon+nodestatus+$IPADDR TARGET=_blank>$IPADDR</A></TD></TR>
          <TR><TD>Sipman reccord type:</TD><TD>$SIPMANRECTYPE</TD></TR>
          <TR><TD>Address type:</TD><TD>$NODETYPE</TD></TR>
          <TR><TD>Management type:</TD><TD>$MANAGEMENTTYPE</TD></TR>
          <TR><TD>Node status:</TD><TD>${NSTATUS:20} (since ${NSTATUS:0:10} ${NSTATUS:11:8})"
    if [ "$USERLEVEL" -gt "4" ]; then
      echo "<A HREF=$SCRIPT_NAME?mon+changestatus+$IPADDR+$NETWORK+Unmanaged>Unmanage</A> -
            <A HREF=$SCRIPT_NAME?mon+changestatus+$IPADDR+$NETWORK+Managed>Manage</A> -
            <A HREF=$SCRIPT_NAME?mon+changestatus+$IPADDR+$NETWORK+Confirmed>Confirm</A>"
    fi
    DNSHOST=`echo $DNSNAME | awk '{ print $2 }'`
    DNSDOMAIN=`echo $DNSNAME | awk '{ print $3 }'`
    DNSTYPE=`echo $DNSNAME | awk '{ print $1 }'`
    echo "</TD></TR>
          <TR VALIGN=top><TD>Dns name:</TD>
	                 <TD><A HREF=$SCRIPT_NAME?named+mon+form-zone+127.0.0.1+$DNSDOMAIN>$DNSHOST.$DNSDOMAIN</A>
			    (type: $DNSTYPE)"
    for FILE in `ls $NETDIR/dnsrec-* 2>/dev/null` `ls $SIM_DNSDIR/cname-${DNSNAME[1]}.${DNSNAME[2]}-* 2>/dev/null`
    do
      HOST=`cat $FILE | awk '{ print $2 }'`
      DOMAIN=`cat $FILE | awk '{ print $3 }'`
      TYPE=`cat $FILE | awk '{ print $1 }'`
      echo "<BR><A HREF=$SCRIPT_NAME?named+mon+form-zone+127.0.0.1+$DOMAIN>$HOST.$DOMAIN</A>"
      #echo "<BR>`cat $FILE | awk '{ print $2 "." $3 " ( " $1 " ) " }'`"
    done
    echo "</TD></TR>
          <TR><TD VALIGN=top>Dns current:</TD><TD>"
          $SIM_HOST $IPADDR | while read V1 V2 V3 V4 NAME
          do
            printf "<TABLE><TR><TD>inaddr:</TD><TD><PRE>$NAME</PRE></TD></TR>"
            if [ "$NAME" != "3(NXDOMAIN)" ]; then
              printf "<TR><TD VALIGN=top>lookup:</TD><TD><PRE>`$SIM_HOST $NAME`</PRE></TD></TR>"
            fi
            printf "</TABLE>"
            
          done
    echo "</TD></TR>"
    if [ "$USERLEVEL" -gt "4" ]; then
      echo "<TR><TD>Community string:</TD><TD>$COMM</TD></TR>
            <TR><TD>Hostname:</TD><TD>"
    fi
    if [ "$HOSTNAME" != "-" ]; then
      echo " snmp: $HOSTNAME"
    fi
    if [ "$DHCPNAME" != "-" ]; then
      echo " dhcp: $DHCPNAME"
    fi
    echo "</TD></TR>"
    if [ "$SNMPCONTACT" != "" ]; then
      echo "<TR><TD>Snmp contact:</TD><TD>$SNMPCONTACT</TD></TR>"
    fi
    if [ "$SNMPLOCATION" != "" ]; then
      echo "<TR><TD>Snmp location:</TD><TD>$SNMPLOCATION</TD></TR>"
    fi
    echo "<TR><TD>OID name:</TD><TD>
          <A HREF=$SCRIPT_NAME?config2+dispoidinfo+$IPADDR+$NETWORK>$SNMPOID</A>
          </TD></TR>
          <TR><TD>Number of interfaces:</TD><TD>Ip: $IPNETNO / Int: $INTNUM</TD></TR>
          <TR><TD>Switch port:</TD>
              <TD><A HREF=$SCRIPT_NAME?switch+display+$SWITCHNAME TARGET=_blank>${SWITCHINFO[*]}</A> 
	 "
    if [ "$GRAPHINTNAME" != "" ]; then
      echo "<A HREF=$SCRIPT_NAME?graph+interface-graphs+$VENDOR+$SWITCHNAME+$GRAPHINTNAME+day>
                  <IMG SRC=/sipman3/gifs/chart_curve.png BORDER=0></A>"
    fi
    echo "</TD></TR>
          <TR><TD>Last configupdate:</TD><TD>$CFGDATE</TD></TR>
	  <TR><TD><FORM   method=post Action=$SCRIPT_NAME?mon+addNote+$IPADDR+>
	    <INPUT TYPE=submit Value=\"Add note\"></TD><TD><CENTER>
	    <TEXTAREA NAME=\"note\" ROWS=4 COLS=100></TEXTAREA>
	    </FORM>
	  </TD></TR>
         "
    echo "</TABLE>
          </TD></TR>"
    SINFO="`getSipmanInfo $IPADDR`"
    FINFO="`getfixedinfo $IPADDR`"
    MINFO="`getmacinfo $MACADDR HTML`"
    if [ "$SINFO $FINFO $MINFO" != "  " ]; then
      echo "<TR BGCOLOR=$COLORINFO><TD>INFO</TD><TD>
            <TABLE>
            $SINFO
            $FINFO
            $MINFO
            </TABLE>
            </TD></TR>"
    fi
 
    echo "<TR BGCOLOR=$COLORDNS><TD>DNS</TD><TD>"
    DispDnsEntry $IPADDR
    printf "</TD></TR>
          <TR BGCOLOR=$COLORLOG><TD>LOG</TD><TD>"
    FMACDIR="$SIM_MACDATAB/$FIRSTMAC"
    if [ -f $FMACDIR/messages ]; then
#     echo "<TR><TD COLSPAN=2>Node log</TD></TR>"
      echo "Node log"
      DispNodeLogging $FMACDIR/messages
    elif [ -f $NETDIR/messages ]; then
      echo "Ip node"
      DispNodeLogging $NETDIR/messages
    fi
    echo "</TD></TR>"
    DispIpInt $IPADDR $FMACDIR
    DispCdpNeighb $IPADDR $FMACDIR
    echo "</TABLE>"
}
dispDatabase ()
{
  IPADDR=$1
  DATADIR="$SIM_IPDATAB/`echo $IPADDR | sed 's#\.#/#g'`"
  if [ -d "$DATADIR" ]; then
    echo "<TABLE>
          <TR><TD COLSPAN=3 BGCOLOR=#DDDDDD>IP database ($DATADIR)</TD></TR>"
    ls $DATADIR | while read FILE
    do
      EXTEN=`echo $FILE | awk -F- '{ print $1 }'`
#     echo "<TR><TD BGCOLOR=#DDFFFF>$FILE</TD><TD>extension: $EXTEN.</TD></TR>"
      if [ "$FILE" = "messages" ]; then
        echo "<TR><TD BGCOLOR=#DDFFFF>$FILE</TD><TD></TD><TD>messages skipped.</TD></TR>"
      elif [ "`echo $FILE | awk -F- '{ print $1 }'`" = "icmpResponseInfo" ]; then
        echo "<TR><TD BGCOLOR=#DDFFFF>$FILE</TD><TD></TD><TD>skipped.</TD></TR>"
      else
        echo "<TR><TD BGCOLOR=#DDFFDD>$FILE</TD><TD><A HREF=$SCRIPT_NAME?mon+removeFile+$DATADIR/$FILE><IMG SRC=$SIM_ICONDIR/trash.gif border=0 alt=\"Remove file\"></A></TD><TD BGCOLOR=#FFFFDD><PRE>`cat $DATADIR/$FILE`</PRE></TD></TR>"
      fi
    done
    PRINAME=`readFile $DATADIR/dnsname.txt | awk '{ print $2 "." $3 }'`
      echo "<TR><TD COLSPAN=3 BGCOLOR=#DDDDDD>Cname reccords ($SIM_DNSDIR/cname-$PRINAME-)</TD></TR>"
    ls $SIM_DNSDIR/cname-$PRINAME-* | while read FILE
    do
      echo "<TR><TD BGCOLOR=#FFFFDD>`cat $FILE | awk '{ print $2 "." $3 }'`</TD><TD><A HREF=$SCRIPT_NAME?mon+removeFile+$FILE><IMG SRC=$SIM_ICONDIR/trash.gif border=0 alt=\"Remove file\"></A></TD><TD>`cat $FILE`</TD></TR>"
    done
    if [ -f "$SIM_DNSDIR/cname-$PRINAME-*" ]; then
      echo "<TR><TD BGCOLOR=#FFFFDD>hallo</TD><TD></TD></TR>"
    fi
    if [ -f $DATADIR/firstmac.txt ]; then
      FIRSTMAC=`readFile $DATADIR/firstmac.txt`
    elif [ -f $DATADIR/arpmac.txt ]; then
      FIRSTMAC=`readFile $DATADIR/arpmac.txt`
    else
      FIRSTMAC=""
    fi
    if [ "$FIRSTMAC" != "" ]; then
      FMACDIR="$SIM_MACDATAB/$FIRSTMAC"
      echo "<TR><TD COLSPAN=3 BGCOLOR=#DDDDDD>FirstMAC database ($FMACDIR)</TD></TR>"
      ls $FMACDIR | while read FILE
      do
        if [ "$FILE" = "messages" ]; then
          echo "<TR><TD BGCOLOR=#DDFFFF>$FILE</TD><TD>messages skipped.</TD></TR>"
        else
          SKIP=yes
          echo "<TR><TD BGCOLOR=#DDFFFF>$FILE</TD><TD><A HREF=$SCRIPT_NAME?mon+removeFile+$FMACDIR/$FILE><IMG SRC=$SIM_ICONDIR/trash.gif border=0 alt=\"Remove file\"></A></TD><TD BGCOLOR=#FFFFDD><PRE>`cat $FMACDIR/$FILE | sed 's#TD##g'`</PRE></TD></TR>"
        fi
      done
    fi
    if [ -f "$DATADIR/snmpcomm.txt" ]; then
      VENDOR=`cat $DATADIR/$OIDSTRING/vendor`
      HOSTNAME=`readFile $DATADIR/hostname.txt | awk -F. '{ print $1 }'`
      echo "<TR><TD COLSPAN=3 BGCOLOR=#DDDDDD>Hostbased database ($SIM_DEVICED/$VENDOR/$HOSTNAME)</TD></TR>"
      ls $SIM_DEVICED/$VENDOR/$HOSTNAME | while read FILE
      do
        echo "<TR><TD BGCOLOR=#DDFFFF>$FILE</TD><TD><A HREF=$SCRIPT_NAME?mon+removeFile+$SIM_DEVICED/$VENDOR/$HOSTNAME/$FILE><IMG SRC=$SIM_ICONDIR/trash.gif border=0 alt=\"Remove file\"></A></TD><TD BGCOLOR=#FFFFDD><PRE>`cat $SIM_DEVICED/$VENDOR/$HOSTNAME/$FILE | sed 's#TD##g'`</PRE></TD></TR>"
      done
    fi
    echo "</TABLE>"
  else
    echo "No data found for $IPADDR."
  fi
}
displaySubnetTable ()
{
  local NETWORK=$1
  NETNAME=$NETWORK
  if [ "$NETWORK" = "root" ]; then
    NETWORK=""
  fi
  local OPTION=$2
  STARTTIME=`date +%s`
  if [ "$NETWORK" = "search" ]; then
    CACHEFILE=sipmon-searchlist-$REMOTE_USER.txt
  else
    CACHEFILE=cache-subnet-$NETNAME
  fi
  if [ -d "$SIM_IPDATAB/`echo $NETWORK | sed 's#\.#/#g'`" ] || [ "$NETWORK" = "search" ]; then
    echo "<H3>Subnet info for network $NETWORK.</H3>"
    displayColorTable
#   echo "<TABLE>"
#   dispTableHeader $ACTION
    if [ "`find $SIM_TMPDIR -name $CACHEFILE -cmin -60`" = "" ] || [ "$OPTION" = "new" ]; then
      # Show the new version if cache too old or new requested.
      CACHEFILE=$SIM_TMPDIR/$CACHEFILE
#     removeIfExists $CACHEFILE
      CACHETMP=$CACHEFILE-$$
#     walkSubnetEntries "$NETWORK" $CACHETMP
      $SIM_MONITOR2T mon walkSubnetEntries "$NETWORK" $CACHETMP
      mv $CACHETMP $CACHEFILE
    else
      if [ "$NETWORK" != "search" ]; then
        if [ "$NETWORK" = "" ]; then
          NETWORK=root
        fi
        echo "<FONT COLOR=black>Cached subnet file from
              `ls -l $SIM_TMPDIR/$CACHEFILE | awk '{ print $6 " " $7 " " $8 }'` for zone $NETWORK.
            <A HREF=$SCRIPT_NAME?mon+displaysubnet+$SORT+$NETWORK+new>Generate fresh file.</A></FONT>"
      fi
      #dispCacheTable $SIM_TMPDIR/$CACHEFILE $SORT
      $SIM_MONITOR2T mon dispCacheTable $SIM_TMPDIR/$CACHEFILE $SORT
    fi
#   dispTableHeader $ACTION
#   echo "</TABLE>"
    displayColorTable
    STOPTIME=`date +%s`
    echo "Generating this page took: `expr $STOPTIME - $STARTTIME` secconds, (`wc -l $CACHEFILE | awk '{ print $1 }'` lines)
."
  else
    if [ "$NETWORK" != "none" ]; then
      echo "Could not find network $NETWORK, select one from the subnets."
    else
      echo "Select one from the subnets."
    fi
  fi
}

readZone ()
{
  local ZONE=$1
  SERVER=$2
  echo "<H2>zone: $ZONE, server: $SERVER</H2>"
  #host -n -l $ZONE $SERVER | grep "has address" | cut '-d ' -f 4 | nmap -v -sP -iL - -oG - | grep "Status: Up" | while read HOST HAS ADDR IPADDR REST
  $SIM_HOST -l $ZONE $SERVER | grep "has address" | while read HOST HAS ADDR IPADDR REST
  do
    addNewNode $IPADDR
  done
}


scanSubnet ()
{
  IPADDR=$1
  MASK=$2
  if [ "$IPADDR" != "" ]; then
    NETWORK=`echo $IPADDR | awk -F. '{ print $1 "." $2 "." $3 }'`
    echo "Scanning for active nodes on addres range $NETWORK.0/$MASK.<P>"
    # Host: 10.1.1.8 ()       Status: Up
    NUM=1
    while [ "$NUM" != "255" ]
    do
      IPLIST="$IPLIST $NETWORK.$NUM"
      NUM=`expr $NUM + 1`
    done
    $SIM_FPING $IPLIST 2>/dev/null | while read IPADDR IS STATUS
    do
      echo "Tested $IPADDR, status $STATUS.<BR>"
      if [ "$STATUS" = "alive" ]; then
        addNewNode $IPADDR nodisp
      fi
    done
  else
    echo "Oops, no ipaddr found as base for the subnet scan...."
  fi

}
# ------------------------------- Main -------------------------------- 
ACTION=$1
SORT=$2
NETWORK=$3
PARAM=$4
EXTRA1=$5
if [ "$ACTION" = "none" ]; then
  ACTION=subnets
fi
if [ "$SORT" = "" ]; then
  SORT=none
fi
if [ "$NETWORK" = "" ]; then
  NETWORK=none
fi
if [ "$NETWORK" = "none" ]; then
  case $ACTION in
    (getinfo|nodestatus)
      NETWORK=`echo $SORT | awk -F. '{ print $1 "." $2 "." $3 }'` ;;
  esac
fi
if [ "$NETWORK" = "search" ]; then
  TNETWORK=$SORT
else
  TNETWORK=$NETWORK
fi
UPNETWORK=""
FOUND=""
TESTIP=`echo $TNETWORK | sed 's/\./ /g'`
for NUM in 3 2 1 
do
  if [ "${TESTIP[$NUM]}" != "" ]; then
    if [ "$FOUND" = "" ]; then
      FOUND=yes
    else
      UPNETWORK=".${TESTIP[$NUM]}$UPNETWORK"
    fi
  fi
done
UPNETWORK="${TESTIP[0]}$UPNETWORK"
#if [ "$SHOWMENU" != "no" ]; then
#  echo "$SIM_PRINTMENU mon line mon ACTION=$ACTION SORT=$SORT NETWORK=$NETWORK<HR>"
#  echo "<FONT COLOR=#666666>"
#  $SIM_PRINTMENU mon line mon $ACTION $SORT $NETWORK
#  echo "</FONT>"
#  SHOWMENU=no
#fi

# Removed, needs to be regulated in the perl module.
#echo "</TD></TR></TABLE>
#     <CENTER>"

if [ "$ACTION" = "nodeMenu" ]; then
  echo "<H3>Menu for $SORT</H3><TABLE>"
  $SIM_PRINTMENU mon-nodestatus table mon $ACTION $SORT $NETWORK $PARAM
  echo "</TABLE>"
elif [ "$ACTION" = "menu" ]; then
  echo "<TABLE>"
  $SIM_PRINTMENU mon table mon $ACTION $SORT $NETWORK
  echo "</TABLE>"
elif [ "$ACTION" = "log" ]; then
  DISPTYPE=$SORT
  NUMLINES=$NETWORK
  SELECTION=$PARAM
  REPEATS=$EXTRA1
  if [ "$DISPTYPE" = "none" ]; then
    DISPTYPE=default
  fi
  if [ "$NUMLINES" = "none" ]; then
    NUMLINES=25
  fi
  dispMessageLog $DISPTYPE $NUMLINES $SELECTION $REPEATS
elif [ "$ACTION" = "runMtr" ]; then
  runMtr $SORT
elif [ "$ACTION" = "dispDatabase" ]; then
  dispDatabase $SORT
elif [ "$ACTION" = "subnets" ]; then
  displaySubnetHeader $NETWORK
elif [ "$ACTION" = "statusInfo" ]; then
  displaySubnetHeader $NETWORK
  if [ "$NETWORK" = "none" ]; then
    export SIM_DEVICED SIM_MACDATAB
    $SIM_DISPNODESTAT
    dispMessageLog default 100 error
  fi
elif [ "$ACTION" = "displaysubnet" ]; then
  displaySubnetHeader $NETWORK
  OPTION=$4
# displaySubnetTable $NETWORK $OPTION
  displaySubnetTable $NETWORK new
elif [ "$ACTION" = "display" ] || [ "$ACTION" = "info" ]; then
  displaysubnets $ACTION $SORT
  if [ "$NETWORK" = "none" ]; then
    echo "Please select a network above....."
  else
    dispheader $ACTION $SORT $NETWORK $SIPMONDATABASE
    disptable $ACTION $SORT $NETWORK $SIPMONDATABASE
  fi
elif [ "$ACTION" = "ping" ]; then
  displaySubnetHeader $NETWORK
  TMPPINGFILE=$SIM_TMPDIR/nmappingfile-$$
  if [ -f $TMPPINGFILE ]; then
    rm $TMPPINGFILE
  fi
  if [ "$NETWORK" = "search" ]; then
    CACHEFILE=$SIM_TMPDIR/sipmon-searchlist-$REMOTE_USER.txt
  else
#   CACHEFILE=$SIM_TMPDIR/cache-subnet-$NETWORK
    CACHEFILE=$SIM_TMPDIR/ip-$NETWORK
  fi
  if [ -f $CACHEFILE ]; then
    cat $CACHEFILE | while read IPADDR REST
    do
      echo $IPADDR >> $TMPPINGFILE
    done
    echo "<H3>Ping scan on network $NETWORK (`cat $TMPPINGFILE | wc -l` nodes).</H3>"
    echo "$CACHEFILE"
    echo "<TABLE><TR><TD><PRE>"
    $SIM_SUDO $SIM_POLLDEVICET $REMOTE_USER fast file $TMPPINGFILE
    echo "</PRE></TD></TR></TABLE>"
    ACTION=displaysubnet
    displaySubnetTable $NETWORK
  else
    echo "<H3>No cache file found for $NETWORK</H3>$CACHEFILE"
  fi
elif [ "$ACTION" = "changestatus" ]; then
  IPADDR=$SORT
  STATUS=$4
  echo "Setting the entry $IPADDR to $STATUS."
  echo "<PRE>"
  case $STATUS in
    (Unmanaged|Managed|Confirmed) $SIM_SUDO $SIM_UPDATET $REMOTE_USER changeStatus "$IPADDR" "$STATUS" ;;
    (*)                           echo "Oops, status not coded......"
  esac
  echo "</PRE>"
  dispNodeStatus $IPADDR
elif [ "$ACTION" = "refreshCache" ]; then
  echo "<H4>Refresching the dns cache table</H4>"
  if [ "$SIM_MANDOMAIN" != "" ]; then
    echo "</CENTER><PRE>"
    echo "Starting at: `date +%H:%M:%S`for managementdomain $SIM_MANDOMAIN"
    $SIM_SUDO $SIM_STARTER $SIM_MANDOMAIN import-dnsrecs verbose
    echo "Stopping at: `date +%H:%M:%S`"
    echo "</PRE><CENTER>"
  fi
elif [ "$ACTION" = "getinfo" ]; then
  IPADDR=$2
      echo "Starting information gathering tool for $IPADDR.<BR>
            <TABLE><TR><TD><PRE>"
      $SIM_SUDO $SIM_IMPORTINFO pollNode $IPADDR
      echo "</PRE></TD></TR></TABLE><CENTER>"
      dispNodeStatus $IPADDR
elif [ "$ACTION" = "discover" ]; then
  IPADDR=$2
  echo "<PRE>"
  $SIM_SUDO $SIM_DISCNODEST pollNode $IPADDR 
  echo "</PRE>"
elif [ "$ACTION" = "confrrd" ]; then
  IPADDR=$2
  PARAM=$4
  echo "Building the config files needed to fill rrd for $IPADDR.<PRE>"
  $SIM_SUDO $SIM_CONFRRDTOOL confnode $IPADDR 2>&1
  echo "</PRE>"
elif [ "$ACTION" = "confautodns" ]; then
  IPADDR=$2
  echo "<H3>Updating Dns info for interfaces, based on the snmp hostname.</H3>"
  $SIM_AUTODNS $IPADDR
elif [ "$ACTION" = "switchinfo" ]; then
  IPADDR=$2
  NETDIR=$SIM_IPDATAB/`echo $IPADDR | sed 's#\.#/#g'`
  COMM=`readFile $NETDIR/snmpcomm.txt`
  echo "</CENTER><PRE>"
  #$SIM_SUDO $SIM_CAMTOOL entry $IPADDR $COMM
  LANG=en_US
  LC_CTYPE="en_US"
  export SIM_MACDATAB SIM_SWITCHINFO SIM_OIDHELPD LANG LC_CTYPE SIM_LOGGERT
  $SIM_SUDO $SIM_SWITCHTABLET $IPADDR $COMM 2>&1
  echo "</PRE>"
elif [ "$ACTION" = "scansubnet" ]; then
  IPADDR=$2
  MASK=24
  scanSubnet $IPADDR $MASK
elif [ "$ACTION" = "nodestatus" ]; then
  NETFILE=$3
  IPADDR=$2
  dispNodeStatus $IPADDR
elif [ "$ACTION" = "searchform" ]; then
  echo "(enter no searchstring for the old search results)
        <FORM method=post Action=$SCRIPT_NAME?mon+searchCache+search-$REMOTE_USER>
        Search for IP-info:
        <INPUT NAME=searchstring Size=20 Type=text>
        <INPUT TYPE=submit Value=Search>
        </FORM>
        <FORM method=post Action=$SCRIPT_NAME?mon+macsearch>
        Search for Mac-info:
        <INPUT NAME=searchstring Size=20 Type=text>
        <INPUT TYPE=submit Value=Search>
        </FORM>"
elif [ "$ACTION" = "macsearch" ]; then
  if [ "$FORM_searchstring" = "" ]; then
    echo "<H2>Oops, no searchstring found.</H2>"
  else
    cd $SIM_MACDATAB
    echo "</CENTER><TABLE>"
    ls | grep -i "$FORM_searchstring" | while read ENTRY
    do
      printf "<TR><TD>Mac:</TD><TD>$ENTRY</TD></TR>
            <TR><TD></TD><TD><TABLE>"
      ls $ENTRY | while read FILE
      do
        if [ "$FILE" != "messages" ]; then
          printf "<TR><TD><B>$FILE</B></TD><TD>"
	  if [ "${FILE:0:12}" = "snmp-ipaddr-" ] || [ "${FILE:0:11}" = "arp-ipaddr-" ]; then
	    TESTIP=`cat $ENTRY/$FILE`
	    echo "<A HREF=$SCRIPT_NAME?mon+nodestatus+$TESTIP>$TESTIP</A>"
	  else
            cat $ENTRY/$FILE | sed 's/>/\&gt;/g' | sed 's/</\&lt;/g'
	  fi
          echo "</TD></TR>"
        fi
      done
      echo "</TABLE></TD></TR>"
    done
    echo "</TABLE>"
  fi
elif [ "$ACTION" = "whois" ]; then
  IPADDR=$2
  echo "<TABLE><TR><TD><PRE>"
  whois $IPADDR
  echo "</PRE></TD></TR></TABLE>"
elif [ "$ACTION" = "addnode" ]; then
  IPADDR=$SORT
  if [ "$FORM_ipaddr" != "" ]; then
    echo "Adding node $FORM_ipaddr."
    addNewNode $FORM_ipaddr
  elif [ "$IPADDR" != "none" ]; then
    echo "Adding node $IPADDR."
    addNewNode $IPADDR
  elif [ "$FORM_zone" != "" ]; then
    echo "Adding zone $FORM_zone to the imported list ($FORM_dnsserver)."
    if [ "$FORM_dnsserver" != "" ]; then
      readZone $FORM_zone $FORM_dnsserver
    else
      dig $FORM_zone ns | while read ZONE NUM IN NS SERVER
      do
        if [ "$IN $NS" = "IN NS" ]; then
          readZone $FORM_zone $SERVER
        fi
      done
    fi
  elif [ "$FORM_range" != "" ]; then
    scanSubnet $FORM_range 24
  else
    echo "<H4>Add a single node</H4>
          <FORM method=post Action=$SCRIPT_NAME?mon+addnode>
          Add a node with ipaddress:
          <INPUT NAME=ipaddr Size=15 Type=text>
          <INPUT TYPE=submit Value=Add-node>
          </FORM>
          <HR>"
    echo "<H4>Add all nodes from a dns domain</H4>
          <FORM method=post Action=$SCRIPT_NAME?mon+addnode>
          Add nodes from zone:
          <INPUT NAME=zone Size=20 Type=text>
          Dns server:
          <INPUT NAME=dnsserver Size=20 Type=text>
          <INPUT TYPE=submit Value=Add-zone>
          </FORM>
          <HR>"
    echo "<H4>Discover all nodes from an class C range</H4>
          <FORM method=post Action=$SCRIPT_NAME?mon+addnode>
          Add nodes from range:
          <INPUT NAME=range Size=20 Type=text>
          <INPUT TYPE=submit Value=Discover-range>
          </FORM>"
  fi

elif [ "$ACTION" = "search" ]; then
  NETFILE=$3
  SORTKEY=$2
  if [ "$FORM_searchstring" != "" ]; then
    SEARCHSTR=$FORM_searchstring
  else
    SEARCHSTR=""
  fi
  TMPFILE=sipmon-searchlist-$REMOTE_USER.txt
  if [ "$SEARCHSTR" != "" ]; then
    if [ -f "$SIM_TMPDIR/$TMPFILE" ]; then
      rm $SIM_TMPDIR/$TMPFILE
    fi
    if [ ! -f $SIM_SEARCHINDEX ]; then
      echo "Oops, no SearchIndex: $SIM_SEARCHINDEX found."
    fi
    echo "<H3>Searching for: ($SEARCHSTR).</H3>"
    displayColorTable
    echo "<TABLE>"
    dispTableHeader $ACTION
    grep -i -h "$SEARCHSTR" $SIM_SEARCHINDEX | while read IPADDR REST
    do
      displaySubnetEntry $IPADDR $SIM_TMPDIR/$TMPFILE
    done
    dispTableHeader $ACTION
    echo "</TABLE>"
  else
    echo "<H3>Old searchresults.</H3>"
    displayColorTable
    echo "<TABLE>"
    dispTableHeader $ACTION
    #dispCacheTable $SIM_TMPDIR/$TMPFILE $SORTKEY
    $SIM_MONITOR2T mon dispCacheTable $SIM_TMPDIR/$TMPFILE $SORTKEY
    dispTableHeader $ACTION
    echo "</TABLE>"
  fi
  if [ -f "$SIM_TMPDIR/$TMPFILE" ]; then
    displayColorTable
  else
    echo "<H3>No results found for your query.....</H3>"
  fi
elif [ "$ACTION" = "removeFile" ]; then
  echo "Starting the maintenance facility to remove $SORT.<BR>"
  $SIM_SUDO $SIM_MAINTENANCE removeFile $SORT
elif [ "$ACTION" = "list" ]; then
  SORT=$2
  TYPE=$3
  if [ "$TYPE" = "none" ] || [ "$TYPE" = "" ]; then
    if [ -d $SIM_DEVICED ]; then
      cd $SIM_DEVICED
      echo "Device type: "
      ls | while read TYPE
      do
        echo "<A HREF=$SCRIPT_NAME?mon+list+$SORT+$TYPE>$TYPE</A> |"
      done
    else
      echo "Oops, no devices directory found."
    fi
  else
    TMPFILE1=$SIM_TMPDIR/devices-$TYPE-poll-$REMOTE_USER
    CACHEFILE=$SIM_TMPDIR/devices-$TYPE-cache-$REMOTE_USER
    if [ "$SORT" = "none" ]; then
      echo "Buidling device list.....<BR>"
      if [ -f $TMPFILE1 ]; then
        rm $TMPFILE1
      fi
      echo "$TMPFILE1"
      ls $SIM_DEVICED/$TYPE | while read DIR
      do
        INFO=`cat $SIM_DEVICED/$TYPE/$DIR/ipaddr`
        echo $INFO >> $TMPFILE1
      done
    fi
    cp $TMPFILE1 $TMPFILE1-poll
    printf "<PRE>"
    $SIM_SUDO $SIM_POLLDEVICET $REMOTE_USER fast file $TMPFILE1-poll
    echo "</PRE><TABLE>"
    dispTableHeader $ACTION
    if [ "$SORT" = "none" ]; then
      if [ -f $CACHEFILE ]; then
        rm $CACHEFILE
      fi
      cat $TMPFILE1 | while read IPADDR
      do
        displaySubnetEntry $IPADDR $CACHEFILE
      done
    else
      #dispCacheTable $CACHEFILE $SORT 
      $SIM_MONITOR2T mon dispCacheTable $CACHEFILE $SORT
    fi
    dispTableHeader $ACTION
    echo "</TABLE>"
  fi
else
  echo "<H2>Oops monitor action $ACTION not defined</H2>"
fi

# ---------- end ----------
